package com.gang.mars.redisson.Thread;

import lombok.SneakyThrows;

import java.util.concurrent.locks.ReentrantLock;

public class Apple12Lock {

    public static void main(String[] args) {

        Ticket ticket = new Ticket();

        new Thread(ticket).start();
        new Thread(ticket).start();
        new Thread(ticket).start();
        new Thread(ticket).start();
    }

    static class Ticket implements Runnable{

        private int ticketNumber = 10;

        private final ReentrantLock lock = new ReentrantLock();

        @SneakyThrows
        @Override
        public void run() {

            while (true){

                try{

                    lock.lock();

                    if (ticketNumber > 0){
                        Thread.sleep(1000);
                        System.out.println(ticketNumber--);
                    } else {
                        break;
                    }
                }finally {
                    lock.unlock();
                }



            }
        }
    }

}
